const errorTypes = require('../constants/error-type')
const userService = require('../service/userService')
const verifyUser = async (ctx, next) => {
  const { username, password } = ctx.request.body
  if (!username || !password) {
    const error = new Error(errorTypes.USERNAME_OR_PASSWORD_IS_EMPITY)
    return ctx.app.emit('error',error,ctx)
  }
  const result = await userService.getUserByUsername(username)
  console.log(result, 'username');
  if (result.length !== 0) {
    const error = new Error(errorTypes.USERNAME_IS_EXIST)
    return ctx.app.emit('error',error,ctx)
  }
  await next()
}
module.exports = {
  verifyUser
}